let requestUrl;
let pages = [15, 30, 100, 200, 500];

function addHandler() {
	requestUrl = "/chongwu_taozhuang_skill_category/insert";

	$("#skill_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#skill_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/chongwu_taozhuang_skill_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#chongwuId").combobox("setValue", rowData.chongwuId);

		$("#skill_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#skill_list").datagrid("getSelected");

	if(rowData) {
		$("#skill_id").val(rowData.id);
		$("#image").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#name").textbox({
		width: 120,
		required: true
	});

	$("#chongwuId").combobox({
		url: "/chongwu_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img src='" + row.image + "' height='18' />&nbsp;" + row.name;
		},
		onSelect: function(record) {
			let name = $("#name").textbox("getValue")

			if (!name) {
				$("#name").textbox("setValue", record.name + "套装");
			}
		}
	});

	$("#note").textbox({
		width: 320,
		height: 80,
		required: true,
		multiline: true
	});

	$("#skill_dialog").dialog({
		title: " ",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#skill_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#skill_dialog").dialog("close");
						$("#skill_list").datagrid("reload");
					}, error);
				});
			}
		},{
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#skill_dialog").dialog("close");
				$("#skill_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#skill_list", "/chongwu_taozhuang_skill_category/upload");

	fetchUploadDialog("/chongwu_taozhuang_skill_category/updateImageById", function () {
		$("#skill_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#skill_list").datagrid({
		url: "/chongwu_taozhuang_skill_category/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", hidden: true},
			{field: "name", title: "技能名称", align:"center", width: 100},
			{field: "note", title: "技能说明", align:"center", width: 200},
			{field: "image", title: "图片", align: "center", width: 50
				, formatter: function(value) {
					return "<img alt='暂无图片' height='" + size + "' src='" + value + "' />";
				}
			}
		]]
	});

});